package com.wohuijituan.whcsmerchant.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wohuijituan.whcsmerchant.R;

/**
 * 说明：
 * 作者：朱世元
 * 时间： 2016/8/24 9:47
 * 版本：V1.0
 * 修改历史：
 */
public class WebViewActivity extends MyBaseActivity{
    private static final String TAG = "WebViewActivity";
    private WebView webView;
    private ImageButton titleGoBack;
    private TextView titleName;
    private ProgressBar webProgressBar;
    private void assignViews() {
        webView = (WebView) findViewById(R.id.web_view);
        titleGoBack = (ImageButton) findViewById(R.id.title_go_back);
        titleName = (TextView) findViewById(R.id.title_name);
        webProgressBar = (ProgressBar) findViewById(R.id.web_progress_bar);
    }
    private Intent intent;//接受传入的Intent
    private String strUrl = "http://yd.wohui365.com/AppHelp/HelpMain.html";//接受传入的Url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleColor();
        setContentView(R.layout.activity_web_view);
        assignViews();
        initData();
        initView();
        initListener();
    }


    private void initData() {
        intent = getIntent();
        //TODO 接受传入URl
    }

    private void initView() {
        titleGoBack.setVisibility(View.VISIBLE);
        //对WebVIew进行配置
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true); // 支持通过JS打开新窗口
        settings.setUseWideViewPort(true);// 设置此属性，可任意比例缩放
        settings.setSupportZoom(true);
        settings.setLoadsImagesAutomatically(true); // 支持自动加载图片
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setAllowFileAccess(true); // 允许访问文件
        settings.setLoadWithOverviewMode(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);

        webView.loadUrl(strUrl);
    }
    private void initListener() {
        webView.setOnKeyListener(new View.OnKeyListener() {
            //点击后退按钮 让webview后退一页
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction()==KeyEvent.ACTION_DOWN) {
                    if (keyCode==KeyEvent.KEYCODE_BACK&&webView.canGoBack()) {
                        //后退到上一个webview 也就是后退到上一个网页
                        webView.goBack();
                        return true;//已处理
                    }
                }
                return false;
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            // url拦截
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器器加载页面
                view.loadUrl(url);
                // 相应完成返回true
                return true;
                // return super.shouldOverrideUrlLoading(view, url);
            }

            // 页面开始加载
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                webProgressBar.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
            }

            // 页面加载完成
            @Override
            public void onPageFinished(WebView view, String url) {
                webProgressBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }

            // WebView加载的所有资源url
            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
            }

            @Override
            public void onPageCommitVisible(WebView view, String url) {
                super.onPageCommitVisible(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//				view.loadData(errorHtml, "text/html; charset=UTF-8", null);
                titleName.setText("error");
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        });//保证在应用中打开新网页
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            // 处理javascript中的alert
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }
            @Override
            // 处理javascript中的confirm
            public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
                return super.onJsConfirm(view, url, message, result);
            }
            @Override
            // 处理javascript中的prompt
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
                return super.onJsPrompt(view, url, message, defaultValue, result);
            }
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                webProgressBar.setProgress(newProgress);
                if (newProgress == 100) {
                    if(webView.canGoBack()){

                    }else {

                    }
                }
            }
            @Override
            public void onReceivedTitle(WebView view, String title) {
                titleName.setText(title);
                showToast(WebViewActivity.this, TAG + title);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView != null) {
            webView.removeAllViews();
            webView.destroy();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        webView.onPause();
    }
}
